add support for enabling delete protection - #268
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request adds delete protection support for CloudStack instances (virtual machines) and volumes (disks) by introducing a new deleteprotection boolean field to both the cloudstack_instance and cloudstack_disk resources. The feature integrates with CloudStack's API updateVirtualMachine and updateVolume endpoints to enable protection against accidental deletion.
Key Changes:
- Added
deleteprotectionoptional boolean field to instance and disk resource schemas - Implemented delete protection setting via CloudStack update APIs after resource creation
- Updated documentation for both resources with usage notes about limitations when resources are managed by other services
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
cloudstack/resource_cloudstack_instance.go |
Added deleteprotection schema field and implementation to set/update protection via UpdateVirtualMachine API |
cloudstack/resource_cloudstack_disk.go |
Added deleteprotection schema field and implementation to set/update protection via UpdateVolume API |
website/docs/r/instance.html.markdown |
Documented deleteprotection parameter with usage notes and reformatted userdata_id/userdata_details entries |
website/docs/r/disk.html.markdown |
Documented deleteprotection parameter with usage notes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
looks like some checks failed due to connection issues. could you check and trigger them again, please? |
There was a problem hiding this comment.
@Curverneur Thansk for the PR. It looks mostly good. Can you check the comments? It would be great if you can add some tests here as well.
…ction in instance and disk resource
|
Hi @vishesh92, thanks for the feedback. Changes commited as requested. Does the test meet the requirements? Do you have any suggestions for changes? |
|
Hi @vishesh92, the Well, using |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Suppressed comments (4)
cloudstack/resource_cloudstack_instance_test.go:318
- The expected-error regex ends with an unescaped
.which matches any single character, not a literal period. If the actual error message ends exactly withdeleted(no trailing character), this regex won’t match and the test will fail. Consider removing the trailing dot or escaping it (e.g.,\\.) and/or anchoring/making the pattern more tolerant.
// attempt to destroy vm. expected to fail due to delete protection is enabled
Config: fmt.Sprintf(testAccCloudStackInstance_deleteProtection, true),
Destroy: true,
ExpectError: regexp.MustCompile(".*has delete protection enabled and cannot be deleted."),
},
cloudstack/resource_cloudstack_disk_test.go:146
- Same regex issue as the instance test: the unescaped trailing
.matches any character, which can cause a false negative if the error string doesn’t have a trailing character. Remove the trailing dot or escape it so the test is robust.
// attempt to destroy disk. expected to fail due to delete protection is enabled
Config: fmt.Sprintf(testAccCloudStackDisk_deleteProtection, true),
Destroy: true,
ExpectError: regexp.MustCompile(".*has delete protection enabled and cannot be deleted."),
},
cloudstack/resource_cloudstack_instance.go:902
- Error messaging is inconsistent: Create uses "delete protection" (no underscore) while Update uses "delete_protection" (underscore). For clearer, consistent UX (and easier test matching), standardize the phrasing across create/update (prefer using the user-facing attribute name or a consistent human-readable phrase).
// Check if the delete protection has changed and if so, update the deleteprotection
if d.HasChange("delete_protection") {
p := cs.VirtualMachine.NewUpdateVirtualMachineParams(d.Id())
p.SetDeleteprotection(d.Get("delete_protection").(bool))
_, err := cs.VirtualMachine.UpdateVirtualMachine(p)
if err != nil {
return fmt.Errorf(
"Error updating the delete_protection for instance %s: %s", name, err)
}
}
cloudstack/resource_cloudstack_disk_test.go:157
- Correct spelling in comment from 'disabledd' to 'disabled'.
// destroy disk. expected to pass due to disk protection is disabledd
Config: fmt.Sprintf(testAccCloudStackDisk_deleteProtection, false),
d083011 to
67d670e
Compare
- Read delete_protection back from the API in resourceCloudStackInstanceRead and resourceCloudStackDiskRead so the Computed attribute reflects import and out-of-band changes instead of always going stale. - Fix TestAccCloudStackInstance_deleteProtection using the disk CheckDestroy helper instead of the instance one. - Escape the trailing "." in the ExpectError regexes so they match a literal period instead of any character. - Standardize the update error message wording to "delete protection" and fix a "disabledd" typo in test comments.
# Conflicts: # cloudstack/resource_cloudstack_instance_test.go
sudo87
left a comment
There was a problem hiding this comment.
Verification summary — PR #268 against the CloudStack simulator
Automated acceptance tests (ran against apache/cloudstack-simulator:4.22.1.0):
TestAccCloudStackDisk_deleteProtection — PASS (4.95s)
TestAccCloudStackInstance_deleteProtection — PASS (23.96s)
Ran scenarios against simulator
Description
Add support for enabling the delete protection for instances (virtual machines) and volumes (disks).
Reason
This feature allows users to enable the delete protection for certain resources to prevent accidental deletion.
References
Changes
Added deleteprotection option to
cloudstack_instanceandcloudstack_diskresource and updated the corresponding documentation pages.cloudstack/resource_cloudstack_instance.gocloudstack/resource_cloudstack_disk.gowebsite/docs/r/instance.html.markdownwebsite/docs/r/disk.html.markdown